﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Silverlight.Hosting.Private;
using System.Drawing;
using Silverlight.Hosting.Interop;
using System.IO;

namespace Silverlight.Hosting
{
    public class SilverlightHost : XcpContainer, ISilverlightHost
    {
        public SilverlightHost() : base(false)
        {
        }

        #region Public Events

        /// <summary>
        /// Raised when the Silverlight control is created.
        /// </summary>
        public new event EventHandler Created;

        /// <summary>
        /// Raised whenever the XAML DOM has successfully loaded (i.e. the application
        /// is fully loaded and ready to display). This notification is useful because
        /// loading Silverlight can take longer for complex applications and when loading
        /// the CLR for the first time. 
        /// </summary>
        public event EventHandler SilverlightLoaded;

        /// <summary>
        /// Raised whenever errors are raised from within the XAML DOM of the hosted control.
        /// This event can indicate when a load failure has occurred, for example, when the
        /// expected .xap file does not exist. You can also use this event to respond to XAML
        /// parsing errors and runtime errors while using the JavaScript API for Silverlight.
        /// </summary>
        public event EventHandler<SilverlightErrorEventArgs> SilverlightError;

        /// <summary>
        /// Raised whenever events occur within the object model of the hosted control.
        /// </summary>
        public event EventHandler<SilverlightEventArgs> SilverlightEvent;

        /// <summary>
        /// Raised when the source download has finished.
        /// </summary>
        public event EventHandler SourceDownloadComplete;

        /// <summary>
        /// Raised when the source download progress changes.
        /// </summary>
        public event EventHandler<SourceDownloadProgressChangedEventArgs> SourceDownloadProgressChanged;

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets a value indicating whether the control is windowless.
        /// </summary>
        public bool IsWindowless
        {
            get { return false; }
        }

        #endregion

        #region Protected Properties

        /// <summary>
        /// Gets the Silverlight plug-in host
        /// </summary>
        protected override ISilverlightHost Host
        {
            get { return this; }
        }
        
        #endregion

        #region Protected Methods

        /// <summary>
        /// Called when the Silverlight control is created.
        /// </summary>
        protected override void OnCreated(EventArgs e)
        {
            base.OnCreated(e);
            if (Created != null)
            {
                Created(this, e);
            }
        }

        /// <summary>
        /// Called whenever the XAML DOM has successfully loaded.
        /// </summary>
        protected override void OnSilverlightLoaded(EventArgs e)
        {
            base.OnSilverlightLoaded(e);
            if (SilverlightLoaded != null)
            {
                SilverlightLoaded(this, e);
            }
        }

        /// <summary>
        /// Called whenever errors are raised from within the XAML DOM of the hosted control.
        /// </summary>
        /// <param name="e">The details of the error.</param>
        protected override void OnSilverlightError(SilverlightErrorEventArgs e)
        {
            base.OnSilverlightError(e);
            if (SilverlightError != null)
            {
                SilverlightError(this, e);
            }
        }

        /// <summary>
        /// Invoked whenever events occur within the object model of the hosted control.
        /// </summary>
        /// <param name="e">The details of the event.</param>
        protected override void OnSilverlightEvent(SilverlightEventArgs e)
        {
            base.OnSilverlightEvent(e);
            if (SilverlightEvent != null)
            {
                SilverlightEvent(this, e);
            }
        }

        /// <summary>
        /// Called when the source download has finished.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSourceDownloadComplete(EventArgs e)
        {
            base.OnSourceDownloadComplete(e);
            if (SourceDownloadComplete != null)
            {
                SourceDownloadComplete(this, e);
            }
        }

        /// <summary>
        /// Called when the source download progress changes.
        /// </summary>
        /// <param name="e">The details of the download progress.</param>
        protected override void OnSourceDownloadProgressChanged(SourceDownloadProgressChangedEventArgs e)
        {
            base.OnSourceDownloadProgressChanged(e);
            if (SourceDownloadProgressChanged != null)
            {
                SourceDownloadProgressChanged(this, e);
            }
        }

        #endregion

        #region ISilverlightHostEventProvider

        void ISilverlightHostEventProvider.OnCreated(EventArgs e)
        {
            OnCreated(e);
        }

        void ISilverlightHostEventProvider.OnSilverlightLoaded(EventArgs e)
        {
            OnSilverlightLoaded(e);
        }

        void ISilverlightHostEventProvider.OnSilverlightError(SilverlightErrorEventArgs e)
        {
            OnSilverlightError(e);
        }

        void ISilverlightHostEventProvider.OnSilverlightEvent(SilverlightEventArgs e)
        {
            OnSilverlightEvent(e);
        }

        void ISilverlightHostEventProvider.OnSourceDownloadComplete(EventArgs e)
        {
            OnSourceDownloadComplete(e);
        }

        void ISilverlightHostEventProvider.OnSourceDownloadProgressChanged(SourceDownloadProgressChangedEventArgs e)
        {
            OnSourceDownloadProgressChanged(e);
        }

        void ISilverlightHostEventProvider.OnContentLoaded(EventArgs e)
        {
            OnContentLoaded(e);
        }

        void ISilverlightHostEventProvider.OnContentError(ErrorEventArgs e)
        {
            OnContentError(e);
        }

        void ISilverlightHostEventProvider.OnContentZoomed(EventArgs e)
        {
            OnContentZoomed(e);
        }

        void ISilverlightHostEventProvider.OnContentResized(EventArgs e)
        {
            OnContentResized(e);
        }

        void ISilverlightHostEventProvider.OnFullScreenChanged(EventArgs e)
        {
            OnFullScreenChanged(e);
        }

        DownloadResult ISilverlightHostEventProvider.DownloadUrl(string url, IDownloadUrlCallback callback, out Stream stream)
        {
            return DownloadUrl(url, callback, out stream);
        }

        #endregion

    }
}
